package com.vvlive.caibao.smartcity.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.vvlive.caibao.smartcity.adapter.FriendsAdapter;
import com.vvlive.caibao.smartcity.bean.Person;
import com.vvlive.caibao.smartcity.data.BaseData;
import com.vvlive.caibao.smartcity.ui.MomentsActivity;
import com.vvlive.caibao.smartcity.ui.PersonDetailsActivity;
import com.vvlive.caibao.smartcity.ui.PersonalMomentsActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/8/20.
 */
public class FriendsFragment extends Fragment {

    private static final int TAB_ONE = 0;
    private static final int TAB_TWO = 1;
    private static final int TAB_THREE = 2;

    private View mView;
    private RadioButton mTabConversations;
    private RadioButton mTabFriends;
    private RadioButton mTabMoments;

    private ViewPager mPager;

    private LayoutInflater inflater;

    private List<View> mPagersList;
    private List<RadioButton> mTabs;

    private Context mContext;

    private TabsClickListener mTabsClickListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        mView = getActivity().getLayoutInflater().inflate(R.layout.activity_enterprisecontacts, null, false);
        initView();
        return mView;
    }

    private void initView() {

        mTabsClickListener = new TabsClickListener();

        mTabConversations = (RadioButton) mView.findViewById(R.id.contact_tab_conversations);
        mTabConversations.setTag(TAB_ONE);
        mTabFriends = (RadioButton) mView.findViewById(R.id.contact_tab_friends);
        mTabFriends.setTag(TAB_TWO);
        mTabMoments = (RadioButton) mView.findViewById(R.id.contact_tab_moments);
        mTabMoments.setTag(TAB_THREE);

        mTabConversations.setOnClickListener(mTabsClickListener);
        mTabFriends.setOnClickListener(mTabsClickListener);
        mTabMoments.setOnClickListener(mTabsClickListener);

        mPager = (ViewPager) mView.findViewById(R.id.contact_pager);
        inflater = getActivity().getLayoutInflater();

        mTabs = new ArrayList<>();
        mTabs.add(mTabConversations);
        mTabs.add(mTabFriends);
//        mTabs.add(mTabMoments);

        mPagersList = getPagers();
        mPager.setAdapter(new ContactPagerAdapter());
        mPager.setCurrentItem(1);
        mPager.setOnPageChangeListener(new ContactPagerChangeListener());

    }

    private List<View> getPagers() {
        List<View> pagers = new ArrayList<>();
        pagers.add(getConversationsPager());
        pagers.add(getFriendsPager());
//        pagers.add(getMomentsPager());
        return pagers;
    }

    private View getMomentsPager() {
        View view = inflater.inflate(R.layout.pager_contact_moments, null, false);
        Button toMoment = (Button) view.findViewById(R.id.contact_pager_moments_to_moments);
        Button toMine = (Button) view.findViewById(R.id.contact_pager_moments_to_mine);
        MomentsPagerClickListener momentsPagerClickListener = new MomentsPagerClickListener();
        toMoment.setOnClickListener(momentsPagerClickListener);
        toMine.setOnClickListener(momentsPagerClickListener);
        return view;
    }

    /**
     * @return
     */
    private View getFriendsPager() {
        View view = inflater.inflate(R.layout.pager_contact_friends, null, false);
        ListView listView = (ListView) view.findViewById(R.id.pager_contact_friends_list);
        listView.setAdapter(new FriendsAdapter(mContext, BaseData.getPagerContacts()));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object obj = parent.getItemAtPosition(position);
                if (obj instanceof Person) {
                    Bundle data = new Bundle();
                    data.putSerializable("person", (Serializable) obj);
                    newActivity(PersonDetailsActivity.class, data);
                } else {
                    toast(parent.getItemAtPosition(position).toString());
                }
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Object obj = parent.getItemAtPosition(position);
                if (obj instanceof Person) {
//                    alert(obj);
                }
                return false;
            }
        });
        FriendsPagerClickListener friendsPagerClickListener = new FriendsPagerClickListener();
        Button fab = (Button) view.findViewById(R.id.pager_contact_friends_fab);
        fab.setOnClickListener(friendsPagerClickListener);
        return view;
    }

    /**
     * @return
     */
    private View getConversationsPager() {
        View view = inflater.inflate(R.layout.pager_contact_conversations, (ViewGroup) null, false);
        return view;
    }

    /**
     * Tabs click listener,
     */
    private final class TabsClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem((int) v.getTag(), true);
        }
    }

    private final class FriendsPagerClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            toast("添加好友");
        }
    }

    private final class MomentsPagerClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.contact_pager_moments_to_moments:
                    newActivity(MomentsActivity.class);
                    break;
                case R.id.contact_pager_moments_to_mine:
                    newActivity(PersonalMomentsActivity.class);
                    break;
            }
        }
    }

    private Intent mIntent;

    private void newActivity(Class cls) {
        newActivity(cls, null);
    }

    private void newActivity(Class cls, Bundle data) {
        if (mIntent == null) {
            mIntent = new Intent();
        }
        if (data != null) {
            mIntent.putExtras(data);
        }
        mIntent.setClass(mContext, cls);
        startActivity(mIntent);
    }

    private Toast mToast;

    private void toast(Object obj) {
        String msg = obj.toString();
        if (mToast == null) {
            mToast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT) ;
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }


    /**
     *
     */
    private final class ContactPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mPagersList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mPagersList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View pager = mPagersList.get(position);
            container.addView(pager, 0);
            return pager;
        }
    }

    /**
     * 监听ViewPager的滑动
     */
    private final class ContactPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            mTabs.get(position).setChecked(true);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }
}
